//
//  CommentCell.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "CommentCell.h"
#import "PostDetailImgsView.h"
#import "ZKTextImgReplaceHandle.h"
@interface CommentCell ()
{
    
    UIImageView *_iconImgView; // 头像
    UILabel *_nickLabel; // 昵称
    UILabel *_nubLabel;//楼层
    UILabel *_createTimeLabel; // 发表时间
    UIButton *_commentImgButton; // 评论图片
    UILabel *_contentLabel; // 帖子内容
    PostDetailImgsView *_postImgsView; // 图片
    UIView *_bgView;
    UILabel *_replyLabel1;
    UILabel *_replyLabel2;
    UILabel *_moreShowLabel;
    UIButton *_deleButton;
}

@end

@implementation CommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self _initViews];
    }
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    
    // 头像
    _iconImgView = [[UIImageView alloc] init];
    _iconImgView.image = [UIImage imageNamed:@"person_message_disabled"];
    _iconImgView.clipsToBounds = YES;
    [self.contentView addSubview:_iconImgView];
    
    // 昵称
    _nickLabel = [[UILabel alloc] init];
    _nickLabel.font = FONT(15);
    _nickLabel.text = @"发帖人昵称";
    _nickLabel.textColor = rgba(12, 91, 158, 1);
    _nickLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_nickLabel];
    
    // 楼主
//    _louImage = [[UIImageView alloc] init];
//    _louImage.image = [UIImage imageNamed:@"论坛首页-详情页-楼主"];
//    [self.contentView addSubview:_louImage];
    _louLabel = [[UILabel alloc] init];
    _louLabel.layer.cornerRadius = 2.5;
    _louLabel.layer.masksToBounds = YES;
    _louLabel.backgroundColor = rgba(12, 91, 158, 1);
    _louLabel.text = @"楼主";
    _louLabel.textColor = [UIColor whiteColor];
    _louLabel.textAlignment = NSTextAlignmentCenter;
    _louLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:_louLabel];
    
    //楼层
    _nubLabel = [[UILabel alloc] init];
    _nubLabel.text = [NSString stringWithFormat:@"第1楼"];
    _nubLabel.textColor = [UIColor lightGrayColor];
    _nubLabel.font = FONT(12);
    [self.contentView addSubview:_nubLabel];
    
    // 发表时间
    _createTimeLabel = [[UILabel alloc] init];
    _createTimeLabel.font = FONT(12);
    _createTimeLabel.text = @"2016-3-1 10:10:10";
    _createTimeLabel.textColor = [UIColor lightGrayColor];
    _createTimeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_createTimeLabel];
    
    // 回复
    _commentImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_commentImgButton setImage:[UIImage imageNamed:@"论坛_进入论坛_评论"] forState:UIControlStateNormal];
    [_commentImgButton addTarget:self action:@selector(reply:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_commentImgButton];
    
    // 删除评论按钮
    _deleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_deleButton addTarget:self action:@selector(deleIt:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_deleButton];

    // 内容
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = FONT(15);
    _contentLabel.numberOfLines = 0;
    _contentLabel.text = @"评论内容评论内容评论内容评论内容";
    _contentLabel.textColor = [UIColor darkGrayColor];
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_contentLabel];
    // 图片视图
    _postImgsView = [[PostDetailImgsView alloc] init];
    _postImgsView.imgAry = @[];
    [self.contentView addSubview:_postImgsView];
    
    //回复背景视图
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self.contentView addSubview:_bgView];
    //显示回复的第一条
    _replyLabel1 = [[UILabel alloc] init];
    _replyLabel1.numberOfLines = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMoreReply:)];
    [_bgView addGestureRecognizer:tap];
    //显示回复的第二条
    _replyLabel2 = [[UILabel alloc] init];
    _replyLabel2.numberOfLines = 0;
    _moreShowLabel = [[UILabel alloc] init];
    [_bgView addSubview:_replyLabel1];
    [_bgView addSubview:_replyLabel2];
    [_bgView addSubview:_moreShowLabel];
    
}


#pragma mark - 重写set
- (void)setLayout:(CommentCellFrameLayout *)layout {
    
    _layout = layout;
    
    [self _setAttributeAndLayout];
}

// 设置属性和布局
- (void)_setAttributeAndLayout {
    
    CommentModel *model = _layout.model;
    
    // 设置头像
    _iconImgView.frame = _layout.iconRect;
    NSURL *url;
    if (model.authorImage != nil) {
        url = [[NSURL alloc]initWithString:model.authorImage];
    }
    [_iconImgView setImageWithURL: url placeholderImage:[UIImage imageNamed:@"defaultImage"]];
    // 设置头像圆角
    _iconImgView.layer.cornerRadius = _layout.iconRect.size.width / 2;
    
    // 设置昵称
    _nickLabel.frame = _layout.nickRect;
    _nickLabel.text = model.nickname;
    if ([_layout.model.isAllowpost intValue] == 0 && _layout.model != nil) {
        _nickLabel.text = @"禁言用户";
        _nickLabel.textColor = [UIColor lightGrayColor];
        _nickLabel.frame = _layout.nickRect2;
    }
    //楼主
//    _louImage.frame = _layout.louImageRect;
    _louLabel.frame = _layout.louImageRect;
    
    //设置楼层
    _nubLabel.frame = _layout.nubRect;
    _nubLabel.text = _layout.model.nuber;
    // 设置时间
    _createTimeLabel.frame = _layout.createTimeRect;
    _createTimeLabel.text = model.dateline_show;

    // 设置评论
    _commentImgButton.frame = _layout.commentImgRect;
    _commentImgButton.tag = [_layout.model.pid intValue];
    
    //删除评论按钮
    _deleButton.frame = _layout.deleButtonRect;
    _deleButton.tag = [_layout.model.pid intValue];
    [_deleButton setTitle:@"删除" forState:UIControlStateNormal];
    _deleButton.titleLabel.font = FONT(13);
    [_deleButton setTitleColor:[UIColor colorWithR:12 g:91 b:158] forState:UIControlStateNormal];
    UserModel *user = [CommonCache getUserInfo];
    if ((user.uid == _layout.model.author_id) ||([user.allowdelpost intValue] == 1)){
        _deleButton.hidden = NO;
    }else{
        _deleButton.hidden = YES;
    }
    // 设置内容
    _contentLabel.frame = _layout.contentRect;
    NSMutableAttributedString *attr = [ZKTextImgReplaceHandle handleText:model.content withRegex:nil];
    if (attr) {
        _contentLabel.attributedText = attr;
    } else {
        _contentLabel.text = model.content;
    }
    // 图片
    _postImgsView.imgHeightAry = _layout.imgHeightAry;
    _postImgsView.imgAry = model.imgAry;
    _postImgsView.frame = _layout.imgRect;
    
    
    //回复视图内的显示
    _bgView.tag = [_layout.model.pid intValue];
    _replyLabel1.frame = _layout.replyLabRect1;
    NSMutableAttributedString *attr1 = [ZKTextImgReplaceHandle handleText:model.reply1 withRegex:nil];
    if (attr1) {
        _replyLabel1.attributedText = attr1;
    } else {
        _replyLabel1.text = model.reply1;
    }
    _replyLabel1.font = FONT(13);
    _replyLabel2.frame = _layout.replyLabRect2;
    NSMutableAttributedString *attr2 = [ZKTextImgReplaceHandle handleText:model.reply2 withRegex:nil];
    if (attr2) {
        _replyLabel2.attributedText = attr2;

    }else{
        _replyLabel2.text = model.reply2;
    }
    _replyLabel2.font = FONT(13);
    _moreShowLabel.frame = _layout.showMoreButtonRect;
    _moreShowLabel.font = FONT(13);
    _moreShowLabel.textColor = [UIColor colorWithR:12 g:91 b:158];
    _moreShowLabel.text = @"显示更多评论";
    _moreShowLabel.textAlignment = NSTextAlignmentCenter;
    if (model.comment == 0) {
        _bgView.hidden = YES;
    }else{
        _bgView.hidden = NO;
        if ([model.comment_num intValue] == 1) {
            _replyLabel2.hidden = YES;
            _moreShowLabel.hidden = YES;
        }else if ([model.comment_num intValue] == 2){
            _replyLabel2.hidden = NO;
            _moreShowLabel.hidden = YES;
        }else{
            _replyLabel2.hidden = NO;
            _moreShowLabel.hidden = NO;
        }
        _bgView.frame = _layout.bgViewRect;
    }

}


- (void)reply:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(replyButtonDid:)]) {
        [self.delegate replyButtonDid:button];
    }
}
- (void)showMoreReply:(UITapGestureRecognizer *)tap{
    if ([self.delegate respondsToSelector:@selector(toShowMoreRe:)]) {
        [self.delegate toShowMoreRe:tap.view.tag];
    }
}
- (void)deleIt:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(deleComment:)]) {
        [self.delegate deleComment:button];
    }

}
@end
